﻿using Crucible.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace Crucible.Controllers
{
    public class ArgumentController : Controller
    {

        private ArgumentConnection db = new ArgumentConnection();

        [HttpPost]
        public ActionResult GetGraph(FormCollection collection)
        {
            int id = 0;
            if (int.TryParse(collection.Get("id"), out id))
            {

                return Json(AssertionHelper.GetTreeObject(id, -1));
            }

            return HttpNotFound();
        }

        [HttpPost]
        public ActionResult GetChildren(FormCollection collection)
        {
            int id = 0;
            if (int.TryParse(collection.Get("id"), out id))
            {
                var children = from link in db.NodeLinks.Where(link => link.Parent_Id == id)
                               select link.Child;

                return Json(children);
            }

            return HttpNotFound();
        }

        [HttpPost]
        public ActionResult GetParents(FormCollection collection)
        {
            int id = 0;
            if (int.TryParse(collection.Get("id"), out id))
            {
                var parents = from link in db.NodeLinks.Where(link => link.Child_Id == id)
                              select link.Parent;

                return Json(parents);
            }

            return HttpNotFound();
        }
 
    }

}
